package com.example.controller;

import com.example.entity.BuildingModel;
import com.example.entity.Household;
import com.example.entity.Page.PageRequest;
import com.example.result.CommonResult;
import com.example.service.HouseholdService;
import com.example.utils.redis.RedisUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @project: smart-property
 * @description: 住户管理 controller 层
 * @author: cjq
 * @create: 2022-11-21 13:53
 **/
@RestController
@RequestMapping("/household")
public class HouseholdController {
    @Autowired
    HouseholdService householdService;

    @Autowired
    RedisUtil redisUtil;

    /**
     * 房屋联级查询
     * @param cid
     * @return
     */
    @GetMapping("/listHouse")
    public List<BuildingModel> listHouse(Integer cid){
        String houseInfo ="houseInfo_"+cid;
        List<BuildingModel> list = (List<BuildingModel>) redisUtil.get(houseInfo);
        if (list == null){
            list = householdService.listHouse(cid);
            redisUtil.set(houseInfo,list);
        }
        return list;
    }

    /**
     * 住户编号查重
     * @param hNumber
     * @param villageId
     * @return
     */
    @GetMapping("/listHnumber")
    public CommonResult listHnumber(String hNumber,Integer villageId){
        return CommonResult.ok(householdService.listHnumber(hNumber,villageId));
    }

    /**
     * 添加/修改住户信息
     * @param household
     * @return
     */
    @PostMapping("/saveHouseHold")
    public CommonResult saveHouseHold(@RequestBody Household household){
        return CommonResult.ok(householdService.saveHouseHold(household));
    }

    /**
     * 住户列表展示
     * @param householdType
     * @param start
     * @param type
     * @param house
     * @param input
     * @param villageId
     * @param startTime
     * @param endTime
     * @param pageRequest
     * @return
     */
    @GetMapping("/queryHouseHold")
    public CommonResult queryHouseHold(String householdType, Integer start,String type,String input,  String startTime, String endTime,String house,Integer villageId,PageRequest pageRequest){
        return CommonResult.ok(householdService.queryHouseHold(householdType,start,type,input,startTime,endTime,house,villageId,pageRequest));
    }

    /**
     * 住户审核
     * @param id
     * @param examineMessage
     * @param start
     * @return
     */
    @DeleteMapping("/examineStart")
    public CommonResult examineStart(Integer id,String examineMessage,Integer start){
        return CommonResult.ok(householdService.examineStart(id,examineMessage,start));
    }

    /**
     * 删除住户信息
     * @param id
     * @return
     */
    @DeleteMapping("/delHouseHold")
    public CommonResult delHouseHold(Integer id){
        return CommonResult.ok(householdService.delHouseHold(id));
    }

    /**
     * 批量删除住户信息
     * @param ids
     * @return
     */
    @PutMapping("/batchDel/{ids}")
    public CommonResult batchDel(@PathVariable Integer[] ids){
        return CommonResult.ok(householdService.batchDel(ids));
    }

    /**
     * 住户统计
     * @param villageId
     * @return
     */
    @GetMapping("/countHouseHoid")
    public CommonResult countHouseHoid(Integer villageId){
        return CommonResult.ok(householdService.countHouseHoid(villageId));
    }
}
